import {
    request
} from "../../request/index.js";
var redis = require("../../utils/redis.js").default;
Page({
    data: {
        goodsObj: [],
        goodsBook: {},
        // 商品是否被收藏
        isCollect: false,
        bindModal: true,
        bookPoint: -1,
        items: [{
                name: '早餐',
                value: '0',
                show: true,
            },
            {
                name: '午餐',
                value: '1',
                show: true,
            },
            {
                name: '晚餐',
                value: '2',
                show: true,
            }
        ]
    },
    GoodsInfo: {},
    onLoad: function (options) {
        const {
            goodsId
        } = options;
        this.getGoodsDetail(goodsId);
        this.isCollect(goodsId);
    },
    async isCollect(goodsId) {
        let userId = redis.get('userId');
        const res = await request({
            url: "/collects/isCollect",
            data: {
                goodsId,
                userId
            }
        });
        console.log("isCollect:" + res.data.data);
        this.setData({
            isCollect: res.data.data
        })
    },
    // 获取商品详情数据
    async getGoodsDetail(goodsId) {
        const res = await request({
            url: "/goods/get",
            data: {
                goodsId
            }
        });
        const goodsObj = res.data.data;
        this.GoodsInfo = goodsObj;
        this.setData({
            goodsObj: {
                attrList: goodsObj.attrList,
                goodsName: goodsObj.goodsName,
                goodsPrice: goodsObj.goodsPrice,
                id: goodsObj.id,
                imageList: goodsObj.imageList
            }
        })
    },
    // 点击轮播图放大预览
    handlePreviewImage(e) {
        // 构造要预览的图片数组
        const urls = this.GoodsInfo.imageList.map(v => v.imageUrl);
        // 接收传递过来的url
        const current = e.currentTarget.dataset.url;
        wx.previewImage({
            urls,
            current
        })
    },
    async handleCartAdd() {
        let cart = wx.getStorageSync('cart') || [];
        let index = cart.findIndex(v => v.id === this.GoodsInfo.id);
        if (index === -1) {
            this.GoodsInfo.num = 1;
            this.GoodsInfo.checked = true;
            cart.push(this.GoodsInfo);
        } else {
            cart[index].num++;
        }
        let goodsId = this.GoodsInfo.id;
        console.log(goodsId);
        const res = await request({
            url: "/carts/addGoods",
            method: "POST",
            data: goodsId
        });
        if(res.data.code === 200) {
            wx.setStorageSync('cart', cart);
            wx.showToast({
                title: '添加成功',
                icon: 'success',
                mask: true
            })
        } else {
            wx.showToast({
                title: '添加失败',
                icon: 'error',
                mask: true
            })
        }
        
    },
    // 点击商品收藏图标
    async handleCollect() {
        // let token = wx.getStorageSync('token');
        const token = redis.get('token');
        if (!token) {
            wx.navigateTo({
                url: '/pages/auth/index'
            });
            return;
        }

        let goodsId = this.GoodsInfo.id;
        let userId = redis.get('userId');

        let isCollect = this.data.isCollect;
        console.log("isCollect:" + isCollect);
        // 收藏过
        if (isCollect) {
            isCollect = false;
            let res = await request({
                url: "/collects/cancelCollect",
                method: 'get',
                data: {
                    goodsId,
                    userId
                }
            })
            if (res.data.code == 200) {
                wx.showToast({
                    title: '取消成功',
                    icon: 'success',
                    mask: true
                });
                isCollect = false;
            } else {
                wx.showToast({
                    title: '取消失败',
                    icon: 'error',
                    mask: true
                });
            }
        } else {
            // 没有收藏过
            let res = await request({
                url: "/collects/collect",
                method: 'get',
                data: {
                    goodsId,
                    userId
                }
            })
            console.log(res.data.code);
            if (res.data.code == 200) {
                wx.showToast({
                    title: '收藏成功',
                    icon: 'success',
                    mask: true
                });
                isCollect = true;
            } else {
                wx.showToast({
                    title: '收藏失败',
                    icon: 'error',
                    mask: true
                });
            }
        }
        // 修改data中的属性  isCollect
        this.setData({
            isCollect
        })
    },
    // 更改模态框显示状态
    bind() {
        this.setData({
            bindModal: !this.data.bindModal
        })
    },
    radioChange(e) {
        var items = this.data.items;
        var currentItemName = e.detail.value;
        for (var i = 0; i < items.length; i++) {
            if (currentItemName.indexOf(items[i].value) != -1) {
                items[i].checked = true;
            } else {
                items[i].checked = false;
            }
        }
        this.setData({
            items: items,
            bookPoint: currentItemName
        })
    },
    async confirm() {
        let bookPoint = this.data.bookPoint;
        if (bookPoint < 0) {
            wx.showToast({
                title: '请选择预订时间！',
                icon: 'none'
            })
            return;
        }
        const token = redis.get('token');
        if (!token) {
            wx.navigateTo({
                url: '/page/login/index'
            });
            return;
        }
        let goodsBook = this.data.goodsBook;
        goodsBook.bookPoint = bookPoint;
        goodsBook.goodsId = this.GoodsInfo.id;
        let res = await request({
            url: "/goodsBooks/bookGoods",
            method: 'post',
            data: goodsBook
        })
        let code = res.data.code;
        if (code == 200) {
            wx.showToast({
                title: '预订成功',
            })
            this.bind();
            this.onShow();
        } else {
            wx.showToast({
                title: res.data.msg,
                icon: 'error'
            })
        }
    }
})